package com.cqut.service.impl;

import com.cqut.VO.task.*;
import com.cqut.entity.CheckTask;
import com.cqut.entity.Task;
import com.cqut.mapper.CheckTaskMapper;
import com.cqut.mapper.TaskMapper;
import com.cqut.service.TaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 作业标题 服务实现类
 * </p>
 *
 * @author 冯永龙
 * @since 2021-06-15
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private CheckTaskMapper checkTaskMapper;

    /**
     * 先想检查作业表添加作业记录，然后在将作业信息添加到作业表中
     *
     * @param task
     * @return
     */
    @Override
    public boolean createTask(Task task) {
        //获取当前课程学生的所有id,并随机生成作业id,在检查作业表中添加学生的作业记录
        List<Integer> studentIdList = taskMapper.getUsersIdByCourseId(task.getCourseId(), 1);
        UUID uuid = UUID.randomUUID();
        String taskId = uuid.toString().substring(0, 15);
        task.setId(taskId);
        for (Integer userId : studentIdList) {
            checkTaskMapper.insertTaskRecord(taskId, userId);
        }
        //设置作业创建时间
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        task.setCreateTime(createTime);
        return taskMapper.createTask(task) == 1;
    }

    @Override
    public List<TaskListVO> findAllTaskByCourseId(Integer id, Integer userType) {
        return taskMapper.findAllTaskByCourseId(id, userType);
    }

    @Override
    public boolean updateTask(Task task) {
        return taskMapper.updateTask(task) == 1;
    }

    @Override
    public boolean deleteTask(String id) {
        taskMapper.deleteTaskRecord(id);
        return taskMapper.deleteTaskById(id) == 1;
    }

    @Override
    public boolean submitTask(CheckTask task) {
        String submitTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        task.setSubmitTime(submitTime);
        if (taskMapper.submitTask(task) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public List<StudentTaskListVO> findStudentTaskList(Integer courseId, Integer userId) {
        return taskMapper.findStudentTaskList(courseId, userId);
    }

    /**
     * 先找到课程的总人数，然后查询每个作业的未提交人数和检查人数
     * 用总人数减去已检查人数便是未检查人数，最后将值赋给TeacherTaskListVO
     *
     * @param courseId
     * @return
     */
    @Override
    public List<TeacherTaskListVO> findTeacherTaskList(Integer courseId) {
        //查询所有作业信息
        List<TaskListVO> taskList = taskMapper.findAllTaskByCourseId(courseId, 0);
        //查询所有学生
        Integer courseTotalStudents = taskMapper.getCourseTotalStudents(courseId);
        //查询没有提交作业的学生列表
        List<TaskNotSubmitNumberVO> notSubmitedNumberList = taskMapper.getTaskNotSubmitedNumber(courseId);
        //查询已经检查的学生作业
        List<TaskCheckedNumberVO> taskNotCheckedNumberList = taskMapper.getTaskCheckedNumberList(courseId);
        List<TeacherTaskListVO> result = new ArrayList<>();
        TeacherTaskListVO teacherTaskListVO;
        for (TaskListVO taskListVO : taskList) {
            teacherTaskListVO = new TeacherTaskListVO();
            //将taskListVO的值赋值给 teacherTaskListVO
            BeanUtils.copyProperties(taskListVO, teacherTaskListVO);
            //处理未提及的学生人数
            for (TaskNotSubmitNumberVO taskNotSubmitNumberVO : notSubmitedNumberList) {
                if (taskListVO.getId().equals(taskNotSubmitNumberVO.getTaskId())) {
                    teacherTaskListVO.setNotSubmited(taskNotSubmitNumberVO.getNotSubmitNumber());
                }
            }
            //处理检查和未检查的人数
            for (TaskCheckedNumberVO taskCheckedNumberVO : taskNotCheckedNumberList) {
                if (taskListVO.getId().equals(taskCheckedNumberVO.getTaskId())) {
                    Integer notCheckedNumber = taskCheckedNumberVO.getNotCheckedNumber();
                    int Checked = courseTotalStudents - notCheckedNumber;
                    teacherTaskListVO.setNotChecked(notCheckedNumber);
                    teacherTaskListVO.setIsChecked(Checked);
                }
            }
            result.add(teacherTaskListVO);
        }
        return result;
    }

    @Override
    public Task getTaskInfo(String taskId) {
        return taskMapper.getTaskInfo(taskId);
    }
}

